/**
 * Created by zzy on 2017/1/13.
 */
var $ = require('jquery');
require('layout/util');

var $alert = require('libs/alert');
require('easyui');
var system = {"2":"CRM","3":"MMS","4":"WMS"};
var html = "";
$(function () {
    var id = $.getUrlParam('id');
    var systemId = $.getUrlParam('systemId');
    var modules;
    var myModules;
    var ajax1 = $.ajax({
        url:"api/v1/p/mms/module/queryModules",
        type:'post',
        data:{systemId:systemId},
        dataType:"json"
    });
    var ajax2 = $.ajax({
        url:"api/v1/p/mms/user/userModules",
        type:"post",
        data:{
            "userId":id,
            "systemId":systemId
        },
        dataType:"json"
    });
    $.when(ajax1,ajax2).done(function (jqXHR1,jqXHR2) {
        var response1 = jqXHR1[0].data;
        var response2 = jqXHR2[0].data;
        if(jqXHR1[0].code==200&&jqXHR2[0].code==200){
            $("#div_module").html("");
            var $target = $("#div_module");
            modulesInit($target,system[systemId],response1,response2);
            $("#button_save").show();
        }else{
            $alert("初始化失败,请联系平台管理员");
        }
    });
    $("#main_form").on("change","input[type=checkbox]",function () {
        if($(this).is(":checked")){
            var $target;
            if($(this).attr('class') == 'level1'){
                $target = $(this).parents('.iteam').find("input[type=checkbox]");
                $target.prop("checked","checked");
            }else{
                $target = $(this).parent().parent().prevAll(":last").children("input[type=checkbox]");
                $target.prop("checked","checked");
                $target.parent().parent().prevAll(":last").children("input[type=checkbox]").prop("checked","checked");
            }
        }else{
            var $target = $(this).parent().parent().find("input[type=checkbox]").not(this);
            $target.prop("checked",false);
        }
    });
    $("#button_save").on("click",function () {
        if($("#main_form input[type=checkbox]:checked").length==0){
            $alert("模块不能为空");
            return false;
        }
        var ids = new Array();
        $.each($("#main_form input[type=checkbox]:checked"),function (k,o) {
            ids.push($(o).val())
        });
        $.ajax({
            url:"api/v1/p/mms/user/connectModule",
            type:"post",
            dataType:"json",
            contentType: "application/json; charset=utf-8",
            data:JSON.stringify({
                "userId":id,
                "ids":ids
            }),
            success:function (result) {
                if(result.code==200){
                    window.location.href="user-manage";
                }else{
                    $alert(result.message)
                }
            }
        })
    })
});
function modulesInit($target,systemName,modules,myModules) {
    $("#system_name").text(systemName);
    modulesRecursion($target,modules,myModules.ids,1);
    $target.html(html);
}
function modulesRecursion($target,modules,myModuleIds,layer) {
    $.each(modules,function (k,o) {
        html = html+"<div class='iteam'>";
        if($.inArray(o.id, myModuleIds)==-1){
            html = html+"<h4><input type='checkbox' class='level1' value='"+o.id+"'>"+ o.moduleName+"</h4>";
        }else{
            html = html+"<h4><input type='checkbox' class='level1' value='"+o.id+"' checked>"+ o.moduleName+"</h4>";
        }
        if(o.childs!=undefined){
            $.each(o.childs,function (k2,o2) {
                html = html+"<div class='list row'>";
                if($.inArray(o2.id, myModuleIds)==-1){
                    html = html+"<h5><input type='checkbox' value='"+o2.id+"'>"+ o2.moduleName+"</h5>";
                }else{
                    html = html+"<h5><input type='checkbox' value='"+o2.id+"' checked>"+ o2.moduleName+"</h5>";
                }
                if(o2.childs!=undefined){
                    $.each(o2.childs,function (k3,o3) {
                        html = html+"<div class='checkbox col-xs-12'>";
                        if($.inArray(o3.id, myModuleIds)==-1){
                            html = html+"<label><input type='checkbox' value='"+o3.id+"'>"+ o3.moduleName+"</label>";
                        }else{
                            html = html+"<label><input type='checkbox' value='"+o3.id+"' checked>"+ o3.moduleName+"</label>";
                        }
                        html = html+"</div>";
                    })
                }
                html = html+"</div>";
            })
        }
        html = html+"</div>";
    })
}
/*function modulesRecursion($target,modules,myModuleIds,layer) {
    var end = false;
    var currLayer = layer;
    $.each(modules,function (k,o) {
        end = false;
        if(o.childs==undefined){
            end = true;
        }
        if(layer==1){
            html = html+"<div class='iteam'>";
            console.log(html)
            html = html+"<h4><input type='checkbox' value='"+o.id+"'>"+ o.moduleName+"</h4>";
            console.log(html)
        }else if(layer==2){
            html = html+"<div class='list row'>";
            console.log(html)
            html = html+"<h5><input type='checkbox' value='"+o.id+"'>"+ o.moduleName+"</h5>";
            console.log(html)
        }else if(layer>2&&end==true){
            html = html+"<div class='checkbox col-xs-12'>";
            console.log(html)
            html = html+"<label><input type='checkbox' value='"+o.id+"'>"+ o.moduleName+"</label>";
            console.log(html)
        }else if(layer>=10){
            $alert("菜单层级必须小于10级");
            return;
        }else{
            //TODO
        }
        if(o.childs!=undefined){
            modulesRecursion($target,o.childs,myModuleIds,++layer);
        }
        html = html+"</div>";
        console.log(html)
    });
}*/
/*
function modulesRecursion($target,modules,myModuleIds,layer) {
    var end = false;
    var currLayer = layer;
    $.each(modules,function (k,o) {
        end = false;
        if(o.childs==undefined){
            end = true;
        }
        if(layer==1){
            $target.append("<div class='iteam'>");
            $target.append("<h4><input type='checkbox' value='"+o.id+"'>"+ o.moduleName+"</h4>");
        }else if(layer==2){
            $target.append("<div class='list row'>");
            $target.append("<h5><input type='checkbox' value='"+o.id+"'>"+ o.moduleName+"</h5>");
        }else if(layer>2&&end==true){
            $target.append("<div class='checkbox col-xs-12'>");
            $target.append("<label><input type='checkbox' value='"+o.id+"'>"+ o.moduleName+"</label>");
        }else if(layer>=10){
            $alert("菜单层级必须小于10级");
            return;
        }else{
            //TODO
        }
        if(o.childs!=undefined){
            modulesRecursion($target,o.childs,myModuleIds,++layer);
        }
        $target.append("</div>");
    })
}*/
